#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "serial.h"
#include <QDateTime>
#include <QTimer>
#include <QDebug>
#include "protocol.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    on_pushButton_refreshCom_clicked(); //更新串口信
    //时间设置
    ui->lineEdit_timeSys->setEnabled(0);
    QDateTime dateTime = QDateTime::currentDateTime();
    ui->dateTimeEdit_timeManual->setDateTime(dateTime);
    QTimer *timeAdvance = new QTimer(this);
    connect(timeAdvance,&QTimer::timeout,this,[=](){
        ui->lineEdit_timeSys->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    });
    timeAdvance->start(1);
}

//刷新串口
void MainWindow::on_pushButton_refreshCom_clicked()
{
    QStringList comList = Serial::instance()->findPort();
    QStringList baudList = Serial::instance()->findBaudRate();
    ui->comboBox_port->clear();
    ui->comboBox_port->addItems(comList);
    ui->comboBox_baud->clear();
    ui->comboBox_baud->addItems(baudList);
    ui->comboBox_baud->setCurrentText("115200");
    ui->pushButton_debugCom->setEnabled(0);
    ui->tabWidget->setEnabled(0);
}
//串口打开、关闭
void MainWindow::on_pushButton_openCom_clicked()
{
    if(!Serial::instance()->isOpen())
    {
        Serial::instance()->open(ui->comboBox_port->currentText(),
                                 ui->comboBox_baud->currentText().toUInt());
        ui->pushButton_openCom->setText("关闭");
    }
    else
    {
        Serial::instance()->close();
        ui->pushButton_openCom->setText("打开");
    }
    ui->comboBox_baud->setEnabled(!Serial::instance()->isOpen());
    ui->comboBox_port->setEnabled(!Serial::instance()->isOpen());
    ui->pushButton_refreshCom->setEnabled(!Serial::instance()->isOpen());
    ui->pushButton_debugCom->setEnabled(Serial::instance()->isOpen());
    ui->tabWidget->setEnabled(Serial::instance()->isOpen());
}

//串口调试界面
#include "comdebugwidget.h"
void MainWindow::on_pushButton_debugCom_clicked()
{
    static ComDebugWidget *debugUI = nullptr;
    if(!debugUI)    //首次打开
    {
        debugUI = new ComDebugWidget(QString("串口-%1 调试界面").arg(ui->comboBox_port->currentText()));
        debugUI->setSerial(Serial::instance());
        debugUI->setGeometry(this->x() + this->width() + 1,this->y(),this->width(),this->height());
        debugUI->show();
    }
    else
        debugUI->setHidden(!debugUI->isHidden());
}

//设置时间
void MainWindow::on_pushButton_timeSet_clicked()
{
    QString strCMD = Protocol::cmd_setTime(ui->dateTimeEdit_timeManual->dateTime());
    Serial::instance()->sendData(strCMD);
}
//时间同步
void MainWindow::on_pushButton_timeSync_clicked()
{
    QString strCMD = Protocol::cmd_setTime(QDateTime::currentDateTime());
    Serial::instance()->sendData(strCMD);
}

//连接wifi
void MainWindow::on_pushButton_wifiConn_clicked()
{
    QString strCMD = Protocol::cmd_ctlWifiConn(1);
    Serial::instance()->sendData(strCMD);
}
//断开wifi
void MainWindow::on_pushButton_wifiDisConn_clicked()
{
    QString strCMD = Protocol::cmd_ctlWifiConn(0);
    Serial::instance()->sendData(strCMD);
}
//设置Wi-Fi
void MainWindow::on_pushButton_wifiSet_clicked()
{
    QString strCMD = Protocol::cmd_setWifi(
                ui->lineEdit_wifiName->text(),
                ui->lineEdit_wifiPwd->text(),
                ui->checkBox_wifiAutoConn->isChecked());
    Serial::instance()->sendData(strCMD);
}
//读取Wifi
void MainWindow::on_pushButton_wifiGet_clicked()
{
    QString strCMD = Protocol::cmd_getWifi();
    Serial::instance()->sendData(strCMD);
}

//设置MQTT
void MainWindow::on_pushButton_mqttSet_clicked()
{
    QString strCMD = Protocol::cmd_setMqtt(
                ui->lineEdit_mqttPath->text(),
                ui->lineEdit_mqttPort->text(),
                ui->lineEdit_mqttName->text(),
                ui->lineEdit_mqttPwd->text(),
                ui->checkBox_mqttAutoConn->isChecked());
    Serial::instance()->sendData(strCMD);
}

//读取MQTT
void MainWindow::on_pushButton_mqttGet_clicked()
{
    QString strCMD = Protocol::cmd_getMqtt();
    Serial::instance()->sendData(strCMD);
}

//切换时间读取
void MainWindow::on_pushButton_changeTimeRead_clicked()
{
    QString strCMD = Protocol::cmd_getSwitchTime();
    Serial::instance()->sendData(strCMD);
}
//切换时间设置
void MainWindow::on_pushButton_changeTimeSet_clicked()
{
    QString strCMD = Protocol::cmd_setSwitchTime(
                ui->lineEdit_waitDrink->text().toUInt(),
                ui->lineEdit_outTime1->text().toUInt(),
                ui->lineEdit_outTime2->text().toUInt(),
                ui->lineEdit_outTime3->text().toUInt(),
                ui->lineEdit_outTime4->text().toUInt(),
                ui->lineEdit_changePeriod->text().toUInt(),
                ui->horizontalSlider_changeTime->value());
    Serial::instance()->sendData(strCMD);
}

void MainWindow::on_lineEdit_changePeriod_textChanged(const QString &arg1)
{
    int max = arg1.toUInt();
    if(max <= 0)
        max = 1;
    ui->horizontalSlider_changeTime->setRange(0,max);
}
void MainWindow::on_horizontalSlider_changeTime_valueChanged(int value)
{
    ui->label_changeTime->setText(QString("%1 s").arg(value));
}

